package com.example.demo;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-10-23
 * Time: 11:27
 */
@RestController
@RequestMapping("/second")
public class Test2Controller {
    @RequestMapping("print1")
    public String print1(String name) {
        return "成功接收参数" + name;
    }

    @RequestMapping("print2")
    public String print2(Integer age) {
        return "成功接收参数" + age;
    }

    @RequestMapping("/print3")
    public String print3(String name,Integer age) {
        return "姓名: " + name +" "+ "年龄: " + age;
    }

    @RequestMapping("/print4")
    public String print4(HttpServletRequest servlet) {
        String name = servlet.getParameter("name");
        Integer age = Integer.valueOf(servlet.getParameter("age"));
        return "姓名: " + name +" "+ "年龄: " + age;
    }

    @RequestMapping("/print5")
    public String print5(String name,int age,String gender) {
        return "姓名: " + name +" "+ "年龄: " + age + " " + "性别: " + gender;
    }

    @RequestMapping("/print6")
    public String print6(User user) {
        return "User类的信息: " + user;
    }

    @RequestMapping("/print7")//参数重命名
    public String print7(@RequestParam("userName") String name) {
        return "姓名: " + name;
    }

    @RequestMapping("/print8")
    public String print8(@RequestParam(value = "userN",required = false)String name) {
        return "姓名: " + name;
    }

    @RequestMapping("/print9")
    public String print9(String[] arr) {
        return "姓名: " + Arrays.toString(arr);
    }

    @RequestMapping("/print10")//传递集合
    public String print10(@RequestParam List<String> list) {
        return "list: " + list;
    }

    @RequestMapping("/print11")//传递Json
    public String print11(@RequestBody User user) {
        return "user: " + user;
    }

    @RequestMapping("/article/{article}")
    public String print12(@PathVariable String article) {
        return "article: "+article;
    }

    @RequestMapping("/article/{article}/{name}")//传递URL参数
    public String print13(@PathVariable("article") String id,@PathVariable String name) {
        return "article: "+ id + " " + "name: " + name;
    }

    @RequestMapping("/print14")//传文件
    public String print14(@RequestPart("fileName") MultipartFile file) {
        return "文件名称: "+ file.getOriginalFilename();
    }

}
